package com.fxm.utils;

import com.fxm.enums.TimeRatioEnum;
import com.google.common.collect.Lists;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeRatioUtil {
    /**
     * 获取上周同一时间（上周周一到上周的今天）
     *
     * @param date
     * @return
     */
    public static Map<String,String> getLastWeekDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //设置周一为一周的起点
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        Date lastWeekMonday = getDayFromToday(cal.getTime(), 7);
        Date lastWeekToday = getDayFromToday(date,7);
        Map<String,String> map = new HashMap<String, String>();
        map.put(TimeRatioEnum.WEEK_MONDAY.getCode(),sdf.format(cal.getTime()));
        map.put(TimeRatioEnum.WEEK_TODAY.getCode(),sdf.format(date));
        map.put(TimeRatioEnum.LAST_WEEK_MONDAY.getCode(),sdf.format(lastWeekMonday));
        map.put(TimeRatioEnum.LAST_WEEK_TODAY.getCode(),sdf.format(lastWeekToday));
        return map;
    }

    /**
     * 获取上年同一时间（上周1月1号到上年的今天）
     *
     * @param date
     * @return
     */
    public static Map<String,String> getLastYearDate(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        String yearBegin = sdf.format(c.getTime()) + "-01-01";
        String yearToday = format.format(c.getTime());
        c.add(Calendar.YEAR, -1);
        String lastYearBegin = sdf.format(c.getTime()) + "-01-01";
        String lastYearToday =format.format(c.getTime());
        Map<String,String> map = new HashMap<String, String>();
        map.put(TimeRatioEnum.YEAR_BEGIN.getCode(),yearBegin);
        map.put(TimeRatioEnum.YEAR_TODAY.getCode(),yearToday);
        map.put(TimeRatioEnum.LAST_YEAR_BEGIN.getCode(),lastYearBegin);
        map.put(TimeRatioEnum.LAST_YEAR_TODAY.getCode(),lastYearToday);
        return map;
    }

    /**
     * 获取今日前number日的日期
     *
     * @param number
     * @return
     */
    public static Date getDayFromToday(Date utilDate,Integer number) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(utilDate);
            c.add(Calendar.DAY_OF_YEAR, -number);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(c.getTime());
            return simpleDateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getBeforeFromToday (Integer number, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_YEAR, number);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(c.getTime());
    }

    /**
     * String date ---> java.util.Date date
     * */
    public static Date stringToUtilDate(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取明天的日期
     *
     * @return
     */
    public static String getDayTomorrow() {
        try {
            Date date = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = simpleDateFormat.format(c.getTime());
            return dateStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getForeCastTime(String hour){
        String tomorrow = TimeRatioUtil.getDayTomorrow();
        StringBuilder sb = new StringBuilder();
        sb.append(tomorrow + " ");
        sb.append(hour);
        sb.append(":00");
        return sb.toString();
    }

    /**
     * 获取最近的12个月份 yyyy-MM
     * @return
     */
    public static List<String> getRecentMonths() {
        List<String> months = Lists.newArrayList();
        for (int i = 0; i < 12; i++) {
            Date beforeOrAfterMonth = TimeUtilCustomize.getBeforeOrAfterMonth(-(i + 1));
            String month = TimeUtilCustomize.parseDateToStr("yyyy-MM", beforeOrAfterMonth);
            months.add(month);
        }
        return months;
    }

    /**
     * 获取上年同月
     * @param date
     * @return
     */
    public static String getBeforeSameMonth(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(date));
            calendar.add(Calendar.MONTH, -12);
            return sdf.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取今日日期
     * @return
     */
    public static String getToday(){
        //获取今日日期
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH,0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TimeUtilCustomize.DATE_DAY_FORMAT);
        String dateStr = simpleDateFormat.format(c.getTime());
        return dateStr;
    }

    /**
     * 获取近30天的日期
     */
    public static List<String> getDayList() {
        try {
            SimpleDateFormat fod = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calc = Calendar.getInstance();
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 30; i++) {
                calc.setTime(new Date());
                calc.add(Calendar.DATE, -i);
                Date minDate = calc.getTime();
                list.add(fod.format(minDate));
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getNow(){
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat endSDF = new SimpleDateFormat(TimeUtilCustomize.DATE_TIME_FORMAT);
        return endSDF.format(c.getTime());
    }

    /**
     * 获取当天0点
     * @return
     */
    public static String getNowDay(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat beginSDF = new SimpleDateFormat(TimeUtilCustomize.DATE_DAY_FORMAT);
        String format = beginSDF.format(c.getTime());
        return format + " 00:00:00";
    }

    /**
     * 获取当前月1号
     * */
    public static String getNowMonth() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat beginSDF = new SimpleDateFormat(TimeUtilCustomize.DATE_MONTH_FORMAT);
        String format = beginSDF.format(c.getTime());
        return format + "-01 00:00:00";
    }

    /**
     * 获取当前年1月1号
     * @return
     */
    public static String getNowYear(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat beginSDF = new SimpleDateFormat(TimeUtilCustomize.DATE_YEAR_FORMAT);
        String format = beginSDF.format(c.getTime());
        return format + "-01-01 00:00:00";
    }

    /**
     * 获取周一
     * @param date
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取最近24小时
     * @return
     */
    public static List<String> getRecent24Hours(){
        List<String> list = Lists.newArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("H");//时间格式化
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        String now = sdf.format(calendar.getTime());
        list.add(now);
        for(int i = 0; i < 23; i++){//当前时间循环24小时减下去
            calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY) - 1);
            String time = sdf.format(calendar.getTime());
            list.add(time);
        }
        return list;
    }

    /**
     * 获取 当前小时 到 0点 如：0,1,2,3,....,23
     * @return
     */
    public static List<String> getNowDayHours(){
        List<String> list = Lists.newArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("H");//时间格式化
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        int hour = Integer.parseInt(sdf.format(calendar.getTime()));
        for (int i = 0; i <= hour; i++) {
            list.add(i + "");
        }
        return list;
    }

    /**
     * 获取 当前小时 到 8点 如：8,9,10,11,12,....,23
     * @return
     */
    public static List<String> getNowDayHoursFromEightAm(){
        List<String> list = Lists.newArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("H");//时间格式化
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        int hour = Integer.parseInt(sdf.format(calendar.getTime()));
        for (int i = 8; i <= hour; i++) {
            list.add(i + "");
        }
        return list;
    }

    /**
     * 获取 时间 到 0点 如：00:00,01:00,....,23:00
     * @return
     */
    public static List<String> getWholeDayHours(){
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                list.add("0" + i + ":00");
            }else {
                list.add(i + ":00");
            }
        }
        return list;
    }

    /**
     * 现在到第二天凌晨的秒数
     * @return
     */
    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 2);
        // 改成这样就好了
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    /**
     * 现在到今天凌晨的秒数
     * @return
     */
    public static Long getSecondsTodayEarlyMorning() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.MILLISECOND,0);
        //当前时间与晚上十二点的毫秒差
        return (calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }


}
